// 导入
const $api = require("../../api/index");
Page({
    data: {
        isShowPlace: true,
        types: [{
                // loadState: 1 loading, 2 ok, 3 nodata
                name: "影院热映", list: [], loadState: 1
            },
            {
                name: "近期热门", list: [], loadState: 1
            },
            {
                name: "免费在线", list: [], loadState: 1
            },
            {
                name: "猜你喜欢", list: [], loadState: 1
            }
        ]
    },

    inputSearchValue(event) {
        var value = event.detail.value;
        var isShowPlace = true;
        if ("" != value) {
            isShowPlace = false;
        }
        this.setData({
            isShowPlace
        });
    },
    doSearchFilms(event) {
        var value = event.detail.value;
        console.log("search-value:", value);
    },
    async loadHomeSuggestion() {
        var loadState = 2;
        try {
            var result = await $api.loadSuggestion(0, 8);
            //console.log('success:', result);
            //console.log('data:', result.data);

            if(result.statusCode == 200){
                this.data.types[3].list = result.data.items;
            } else {
                loadState = 3;
                wx.showToast({
                    title: result.data.localized_message,
                    icon: "error",
                    duration: 2000
                });
            }
        } catch (error) {
            //console.log('error:', error);
            loadState = 3;
            wx.showToast({
                title: '网络异常',
                icon: "error",
                duration: 2000
            });
        }
        this.data.types[3].loadState = loadState;
        this.setData({
            types: this.data.types
        });
    },
    async loadHomeMovieShowing() {
        var loadState = 2;
        try {
            var result = await $api.loadSubjectCollection('movie_showing');
            //console.log('movie_showing:', result);
            if(result.statusCode == 200){
                this.data.types[0].list = result.data.subject_collection_items;
            } else {
                loadState = 3;
                wx.showToast({
                    title: result.data.localized_message,
                    icon: "error",
                    duration: 2000
                });
            }
        } catch (error) {
            console.log('error:', error);
            loadState = 3;
            wx.showToast({
                title: '网络异常',
                icon: "error",
                duration: 2000
            });
        }
        this.data.types[0].loadState = loadState;
        this.setData({
            types: this.data.types
        });
    },
    async loadHomeMovieLatest() {
        var loadState = 2;
        try {
            var result = await $api.loadSubjectCollection('movie_latest');
            //console.log('movie_latest:', result);
            if(result.statusCode == 200){
                this.data.types[1].list = result.data.subject_collection_items;
            } else {
                loadState = 3;
                wx.showToast({
                    title: result.data.localized_message,
                    icon: "error",
                    duration: 2000
                });
            }
        } catch (error) {
            console.log('error:', error);
            loadState = 3;
            wx.showToast({
                title: '网络异常',
                icon: "error",
                duration: 2000
            });
        }
        this.data.types[1].loadState = loadState;
        this.setData({
            types: this.data.types
        });
    },
    async loadHomeMovieFreeStream() {
        var loadState = 2;
        try {
            var result = await $api.loadSubjectCollection('movie_free_stream');
            //console.log('movie_latest:', result);
            if(result.statusCode == 200){
                this.data.types[2].list = result.data.subject_collection_items;
            } else {
                loadState = 3;
                wx.showToast({
                    title: result.data.localized_message,
                    icon: "error",
                    duration: 2000
                });
            }
        } catch (error) {
            console.log('error:', error);
            loadState = 3;
            wx.showToast({
                title: '网络异常',
                icon: "error",
                duration: 2000
            });
        }
        this.data.types[2].loadState = loadState;
        this.setData({
            types: this.data.types
        });
    },
    async loadHomeDatas() {
        await this.loadHomeSuggestion();
        await this.loadHomeMovieShowing();
        await this.loadHomeMovieLatest();
        await this.loadHomeMovieFreeStream();
    },
    onLoad(options) {
        //生命周期函数--监听页面加载
        this.loadHomeDatas();
    },
    async onPullDownRefresh() {
        //页面相关事件处理函数--监听用户下拉动作
        await this.loadHomeDatas();
        wx.stopPullDownRefresh();
    }
})